<?php

define('SY_DIR', dirname(__file__));

global $includePath, $config;

require_once('functions.php');
require_once('l10n.php');

if (! defined('IN_ADMIN')) {
  define('IN_ADMIN', false);
}

sy_autoload_register('autoload');

uses('kernel');
uses('kernel/core');
uses('kernel/data');
uses('kernel/view');
uses('kernel/db');

require_once('kernel/Config.php');

$config = new Config();

$config->set('app_dir', APP_DIR);
$config->set('app_url', Simplify::request()->url());

$config->set('cache_dir', '{app_dir}/cache');

$config->set('views_dir', '{app_dir}/views/{theme}');
$config->set('views_cache_dir', '{cache_dir}/views');

$config->set('public_path', '/public');

$config->set('www_dir', dirname($_SERVER['SCRIPT_FILENAME']) . '{public_path}');
$config->set('www_url', '{app_url}{public_path}');

$config->set('files_dir', '{www_dir}/files');
$config->set('files_url', '{www_url}/files');

$config->set('theme', $config->get('theme', 'default'));
$config->set('theme_url', '{www_url}/{theme}');

$config->set('admin_url',  '{app_url}/admin');

$config['routes'] = array(
  array('/:controller/*', array('controller' => 'home')),
  array('/admin/:controller/*', array('in_admin' => true, 'controller' => 'admin')),
  array('/admin/:path/:controller/*', array('in_admin' => true)),
);

require_once(APP_DIR . '/config.php');

require_once('debug.php');

function uses($api)
{
  global $includePath;

  if (! sy_path_is_absolute($api)) {
    $path = SY_DIR . '/' . $api;
  }
  else {
    $path = $api;
  }

  $includePath[] = $path;

  if (is_dir($path . '/api')) {
    $includePath[] = $path . '/api';
  }

  if (file_exists($path . '/exceptions.php')) {
    require_once($path . '/exceptions.php');
  }

  if (file_exists($path . '/functions.php')) {
    require_once($path . '/functions.php');
  }
}

function autoload($class)
{
  global $includePath;

  foreach ($includePath as $path) {
    $filename = $path . '/' . $class . '.php';

    if (file_exists($filename)) {
      require_once($filename);
      break;
    }
  }
}
